package com.wen.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

/**
 * 描 述: bootstrap组件
 *
 * @author 南瓜
 * @date: 2018/8/13
 */
@Controller
@RequestMapping("/bootstrap")
public class Bootstrap3Controller {
    
    @RequestMapping("index")
    public String index() {
        return "bootstrap/index";
    }
    
    @RequestMapping("upload")
    public String fileUpload() {
        return "bootstrap/fileUpload";
    }
    
    @ResponseBody
    @RequestMapping(value = "/uploadFile")
    public String upload(MultipartFile myFile)
        throws IllegalStateException, IOException {
        System.out.println("-----------------------");
        String name = myFile.getOriginalFilename();
        String path = "";// request.getServletContext().getRealPath("/upload/");// 上传保存的路径
        String fileName = changeName(name);
        String rappendix = "upload/" + fileName;
        fileName = path + "\\" + fileName;
        File file1 = new File(fileName);
        myFile.transferTo(file1);
        String str = "{\"src\":\"" + rappendix + "\"}";
        return str;
    }
    
    public static String changeName(String oldName) {
        Random r = new Random();
        Date d = new Date();
        String newName = oldName.substring(oldName.indexOf('.'));
        newName = r.nextInt(99999999) + d.getTime() + newName;
        return newName;
    }
}
